package com.fastframework.net {

	/**
	 * @author colin
	 */
	public class ResolveLink {
		public static var resourcePath:String="";
		public static var testingServer:String="";

		public static function create (fileName:String):String{
			if(fileName.indexOf("http://")==0){
				return fileName;
			}
			
			if(fileName.indexOf("/")==0){
				if(resourcePath.match('file:///')){
					if(testingServer==""){
						return resourcePath+fileName;
					}else{
						//http://www.digi3studio.com/
						var domain:String = testingServer.split(".com/")[0]+".com";
						return domain+fileName;
					}
				}
				return fileName;
			}
			
			var ext:String = String(fileName.split("?")[0]).split(".")[1];
			var specifyFolder:String = "";
			var noCache:String = "";
			switch(ext){
					case "jpg":
					case "jpeg":
					case "gif":
					case "png":
						specifyFolder = "img/";
						break;
					case "flv":
						specifyFolder = "flv/";
						break;
					case "xml":
						specifyFolder = "xml/";
						break;
					case "php":
						noCache='&r='+Math.floor(Math.random()*10000);
						specifyFolder = "service/";
						break;
			}
			if(fileName.indexOf('_upload_')==0)specifyFolder="";

			
			var path:String = resourcePath+specifyFolder+fileName;
			if(noCache!=""){
				path = path+((path.indexOf('?')==-1)?'?':'')+noCache;
			}

			if(resourcePath.match('file:///')){
				if(testingServer==""){
					path = resourcePath+specifyFolder+(fileName.split("?")[0]);
					trace('local file request:'+path);
				}else{
					path = testingServer+specifyFolder+fileName;
				}
			}

			return path;
		}
		
		public static function setResourcePath(str:String):void{
			if(resourcePath==str)return;
			if(resourcePath!="")trace('warning:resource path '+resourcePath+' change to '+str);
			resourcePath=str;
		}
	}
}
